<?php
namespace App\Http\Controllers;
use App\Models\admin_user;
use Illuminate\Http\Request;

class login_admin extends \Illuminate\Routing\Controller
{
    public function login_admin(Request $request)
    {
        return view('login_admin');
    }
    public function login_admin_ok(Request $request)
    {
        $userName=$request->input('userName');
        if(!$userName=="")
        {
            $pwd=md5($request->input('pwd'));
            $user=new admin_user();
            $user_rows=$user->where('admin_user_name',$userName)->first();
            if(!$user_rows){
                return ['code'=>-1,'name'=>'用户不存在'];
            }else if($user_rows['admin_user_pwd']!=$pwd){
                return ['code'=>-2,'name'=>'密码错误'];
            }else{
                $session=$request->session()->put('admin_user_name',$userName);
                return ['code'=>1,'name'=>'登录成功','url'=>'/admin'];
            }
        }else{
            return ['code'=>-3,'name'=>'用户名不能为空'];
        }
    }
    public function login_admin_out(Request $request)
    {
        $session = $request->session()->forget('admin_user_name');
        if($session==null) {
            return redirect('/login_admin');
        }
    }
}
